<?php
/**
 * Created By PhpStorm
 * Author: patrick wu
 * Date: 2020/9/22
 * Time: 17:34
 */
namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Service\Admin\AttributeService;
use App\Constant\ErrorConstant;
use Illuminate\Support\Facades\Validator;

class AttributeController extends BaseController
{

    protected $service;

    public function __construct(Request $request, AttributeService $service)
    {
        parent::__construct($request);
        $this->service  =   $service;
    }

    /**
     * 属性列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function listAttr()
    {
        $page   =   (int) $this->request->input('page', 1);
        $size   =   (int) $this->request->input('page_size', 20);
        [$flag, $result]    =   $this->service->attrList($page, $size);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_ATTR_LIST_ERROR, $result);
        }
    }

    /**
     * 属性删除
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteAttr()
    {
        $validator  =   Validator::make($this->request->post(), [
            'attr_id'   =>  'required|integer|exists:mini_attribute,id'
        ], [
            'attr_id.required'  =>  '属性ID不能为空',
            'attr_id.integer'   =>  '属性ID必须为整数',
            'attr_id.exists'    =>  '属性不存在'
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_ATTR_DELETE_ERROR, $validator->errors()->first());
        }

        $attr_id    =   (int) $this->request->post('attr_id');
        [$flag, $result]    =   $this->service->attrDelete($attr_id);
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_ATTR_DELETE_ERROR, $result);
        }
    }

    /**
     * 属性添加
     * @return \Illuminate\Http\JsonResponse
     */
    public function addAttr()
    {
        $validator  =   Validator::make($this->request->post(), [
            'attr_name'     =>  'required|string|max:30',
            'attr_en_name'  =>  'required|string|max:100',
//            'sorting'       =>  'required|integer',
//            'is_system'     =>  'required|integer|between:0,1'
        ], [
            'attr_name.required'    =>  '属性名称不能为空',
            'attr_en_name.required' =>  '属性英文名称不能为空',
            'sorting.required'      =>  '属性排序不能为空',
            'is_system.required'    =>  '是否系统属性不能为空',

            'attr_name.string'      =>  '属性名称必须为字符',
            'attr_en_name.string'   =>  '属性英文名称必须为字符',
            'sorting.integer'       =>  '属性排序必须为数字',
            'is_system.integer'     =>  '是否系统属性必须为数字',
            'attr_name.max'         =>  '属性名称过长',
            'attr_en_name.max'      =>  '属性英文名称过长',
        ]);

        if ($validator->fails()) {
            return $this->errorResponse(ErrorConstant::ADMIN_ATTR_ADD_ERROR, $validator->errors()->first());
        }

        [$flag, $result]    =   $this->service->attrAdd($this->request->post());
        if ($flag) {
            return $this->successResponse($result);
        } else {
            return $this->errorResponse(ErrorConstant::ADMIN_ATTR_ADD_ERROR, $result);
        }
    }

}
